﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class UserWork_Plan_user_MyPlan : System.Web.UI.Page
{
    B_UserPlan bll = new B_UserPlan();
    M_userPlan model = new M_userPlan();
    protected void Page_Load(object sender, EventArgs e)
    {
        string strCreater = Session["LoginName"].ToString();
        lblCreater.Text = strCreater;
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            string type = Request.QueryString["type"];
            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(type))
            {
                this.ShowInfo(int.Parse(id), type);
            }
            
        }
    }
    private void ShowInfo(int id, string type)
    {
        if (type.Equals("1"))
        {
            this.lnkSavePlan.Enabled = false;
            this.lnkDelPlan.Enabled = false;
        }
        else
        {
            this.lnkSavePlan.Enabled = true;
        }
        model = bll.GetMode(id);
        txtPlanSubject.Text = model.PlanSubject;
        txtPlanBody.Text = model.PlanBody;
        cboCategory.Text = model.Category;
        cboProgress.Text = model.Progress;
        lblCreater.Text = model.Creater;
    }
    protected void grdvPlan_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    //任务添加事件：此处以后可以添加协作者。
    protected void lnkSavePlan_Click(object sender, EventArgs e)
    {
        string strSubject = this.txtPlanSubject.Text.Trim();
        string strBody = this.txtPlanBody.Text.Trim();
        string strCategory = this.cboCategory.Text.Trim();
        string strProcess = this.cboProgress.Text.Trim();
        string strCrater = this.lblCreater.Text.Trim();
        string strCreateOn = DateTime.Now.Date.ToShortDateString();
        string    strCreateEnd = DateTime.Now.Date.ToShortDateString();
        try
        {
            string id = Request.QueryString["id"];
            string type = Request.QueryString["type"];

            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(type))
            {
                if (type=="2")
                {
                    #region Modify
                    model.ID = int.Parse(id);
                    model.PlanSubject = strSubject;
                    model.PlanBody = strBody;
                    model.Category = strCategory;
                    model.Progress = strProcess;
                    model.Creater = strCrater;
                    model.StartTime = DateTime.Parse(strCreateOn);
                    model.EndTime = DateTime.Parse(strCreateEnd);
                    model.Helper = "";   //关联用户暂时不用
                    if (CheckTime(strCreateOn, strCreateEnd))
                    {
                        if (bll.UpdateUserPlan(model) == 1)
                            Response.Redirect("user_PlanManage.aspx");
                        else
                            Response.Write("创建失败");
                    }
                    else
                    {
                        Response.Write("<script>alert('开始时间不能大于结束时间！')</script>");
                    }
                    #endregion
                }
                else if (type == "3")
                {
                    #region Delete
                    if (CheckTime(strCreateOn, strCreateEnd))
                    {
                        if (bll.DeleteUserPlan(int.Parse(id)) == 1)
                            Response.Redirect("user_PlanManage.aspx");
                        else
                            Response.Write("删除失败");
                    }
                    else
                    {
                        Response.Write("<script>alert('开始时间不能大于结束时间！')</script>");
                    }
                    #endregion
                }
            }
            else
            {
                #region Add
                model.PlanSubject = strSubject;
                model.PlanBody = strBody;
                model.Category = strCategory;
                model.Progress = strProcess;
                model.Creater = strCrater;
                model.StartTime = DateTime.Parse(strCreateOn);
                model.EndTime = DateTime.Parse(strCreateEnd);
                model.Helper = "";   //关联用户暂时不用
                if (CheckTime(strCreateOn, strCreateEnd))
                {
                    if (bll.AddUserPlan(model) == 1)
                    {
                        Response.Write("创建成功");
                        Response.Redirect("user_PlanManage.aspx");
                    }
                    else
                        Response.Write("创建失败1");
                }
                else
                {
                    Response.Write("<script>alert('开始时间不能大于结束时间！')</script>");
                }
                #endregion

            }
        }
        catch
        {
            Response.Write("创建失败2");
        }
    }
    public bool CheckTime(string strBeg, string strEnd)
    {
        if (DateTime.Parse(strBeg) <= DateTime.Parse(strEnd))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}